// 导入自定义类方法
import {
    hospitalList,
    venueList,
    venueDetailMsg,
    ambulanceList,
} from "../../modelUtils/returnModel";

import {
    GetMedicalPointCount,
    GetAmbulanceCount,
    GetUserCount,
    GetHospitalCount
} from '../../network/getBaseInfoNum'

import { QueryMedicalPointList, QueryUserList, QueryAmbulanceList, QueryPatientList } from '../../network/hosVenAmbuListData'
import { setupMapState } from "../../store/utils/hook"
/**
 * 初始状态（值）
 */
const state = {
    hospitalTreeRemoteVx: [], //医院
    hospitalTreeRemoteVxLength: 0,
    venueTreeRemoteVx: [], //场馆
    venueMedicalPointArrVx: [], //所有医疗点
    ambulanceTreeRemoteVx: [], //车辆
    ambulanceStatusVx: "",
    venueDetailVx: [],
    usernameVx: "",
    cityOptionsVx: [],

    athletesVillageClinicNumVx: { total: 0, onLine: 0 },
    // 观众医疗点数量
    audienceMedicalNumVx: { total: 0, onLine: 0 },
    // FOP医疗点数量
    FOPMedicalNumVx: { total: 0, onLine: 0 },
    // 场馆医务室数量
    medicalRoomNumVx: { total: 0, onLine: 0 },
    // 救护车数量
    ambulanceNumVx: { total: 0, onLine: 0 },
    // 医生人数
    doctorNumVx: { total: 0, onLine: 0 },
    // 护士人数
    nurseNumVx: { total: 0, onLine: 0 },
    //医院数量
    hospitalNumVx: 0,
    // 医务室
    medicalRooms: [],
    // FOP医疗点
    FOPMedicalPoints: [],
    // 观众医疗点
    audienceMedicalPoints: [],
    // 医生
    doctorsVx: [],
    // 护士
    nursesVx: [],
    // 车辆
    ambulancesVx: [],
    // 接诊病人
    acceptPatientVx: [],
    // 送往医院
    sendHospitalPatientVx: [],
    // 保存某个场馆
    venueInfoVx: null,
    // 保存某个城市
    cityInfoVx: null,
    // 点击的调度任务
    curTaskRow: {},
    // 非保障车辆数据
    notGuaranteeAmbVx: []
}
const actions = {
    // 医院
    saveHospitalAct: (context, payload) => {
        // console.log(payload)
        context.commit("saveHospitalAct", payload)
    },
    //场馆
    saveVenueAct: (context, payload) => {
        // console.log(payload)
        context.commit("saveVenueAct", payload)
    },
    //车辆
    saveAmbulanceAct: (context, payload) => {
        // console.log(payload)
        context.commit("saveAmbulanceAct", payload)
    },
    // 场馆详情
    saveVenueDetailAct: (context, payload) => {
        // console.log(payload)
        context.commit("saveVenueDetailAct", payload)
    },

    // 城市下拉
    getCityOptionsAct: (context, payload) => {
        context.commit("getCityOptionsAct", payload)
    },

    // 数量
    getMedicalCount({ commit, rootState }, payload) {
        // console.log(payload)
        let { ACVCodesVx, userInfoVx } = rootState.app
        // console.log(ACVCodesVx)
        GetMedicalPointCount({
            activityCode: ACVCodesVx.activityCode,
            cityCode: ACVCodesVx.cityCode,
            venueCode: ACVCodesVx.venueCode == -1 ? userInfoVx.venueCode : ACVCodesVx.venueCode
        }).then(res => {
            // console.log(res)
            // console.log(JSON.stringify(res))
            if (res?.total.length > 0) {
                commit("getMedicalCount", res?.total)
            }
        }).catch(err => {
            console.log(err)
        })
    },

    getAmbulanceCount({ commit, rootState }, payload) {
        // console.log(payload)
        let { ACVCodesVx, userInfoVx } = rootState.app
        // console.log(ACVCodesVx)
        GetAmbulanceCount({
            activityCode: ACVCodesVx.activityCode,
            cityCode: ACVCodesVx.cityCode,
            venueCode: ACVCodesVx.venueCode == -1 ? userInfoVx.venueCode : ACVCodesVx.venueCode
        }).then(res => {
            // console.log(res)
            if (res?.total.length > 0) {
                commit("getAmbulanceCount", res.total)
            }
        }).catch(err => {
            console.log(err)
        })
    },

    getUserCount({ commit, rootState }, payload) {
        // console.log(payload)
        let { ACVCodesVx, userInfoVx } = rootState.app
        GetUserCount({
            activityCode: ACVCodesVx.activityCode,
            cityCode: ACVCodesVx.cityCode,
            venueCode: ACVCodesVx.venueCode == -1 ? userInfoVx.venueCode : ACVCodesVx.venueCode
        }).then(res => {
            // console.log(res)
            if (res?.total.length > 0) {
                commit("getUserCount", res.total)
            }
        }).catch(err => {
            console.log(err)
        })
    },

    getHospitalCount({ commit, rootState }) {
        let { ACVCodesVx } = rootState.app
        GetHospitalCount({
            activityCode: ACVCodesVx.activityCode,
            cityCode: ACVCodesVx.cityCode,
        }).then(res => {
            // console.log(res)
            if (res > 0) {
                commit("getHospitalCount", res)
            }
        }).catch(err => {
            console.log(err)
        })
    },

    // 医疗点数据
    getMedicalRoom({ commit, rootState }, payload) {
        // console.log(payload)
        let { ACVCodesVx } = rootState.app
        QueryMedicalPointList({
            activityCode: ACVCodesVx.activityCode,
            cityCode: ACVCodesVx.cityCode,
            venueCode: ACVCodesVx.venueCode,
            type: payload.type
        }).then(res => {
            // console.log(res)
            commit("getMedicalRoom", { res, type: payload.type })
        }).catch(err => {
            console.log(err)
        })
    },

    // 医生/护士数据
    getUserInfo({ commit, rootState }, payload) {
        // console.log(payload)
        let { ACVCodesVx } = rootState.app
        QueryUserList({
            activityCode: ACVCodesVx.activityCode,
            cityCode: ACVCodesVx.cityCode,
            venueCode: ACVCodesVx.venueCode,
            type: payload.type
        }).then(res => {
            // console.log(res)
            commit("getUserInfo", { res, type: payload.type })
        }).catch(err => {
            console.log(err)
        })
    },

    // 车辆数据
    getAmbulanceInfo({ commit, rootState }, payload) {
        let { ACVCodesVx } = rootState.app
        QueryAmbulanceList({
            activityCode: ACVCodesVx.activityCode,
            cityCode: ACVCodesVx.cityCode,
            venueCode: payload.venueId,
            type: payload.type
        }).then(res => {
            // console.log(res)
            commit("getAmbulanceInfo", { res })
        }).catch(err => {
            console.log(err)
        })
    },

    // 接诊病人数据
    getAcceptPatient({ commit, rootState }, payload) {
        let { ACVCodesVx, userInfoVx } = rootState.app
        // console.log(ACVCodesVx)
        // type == 1 接诊病人
        QueryPatientList({
            activityCode: ACVCodesVx.activityCode,
            cityCode: ACVCodesVx.cityCode,
            venueCode: ACVCodesVx.venueCode == -1 ? userInfoVx.venueCode : ACVCodesVx.venueCode,
            type: payload.type
        }).then(res => {
            commit("getAcceptPatient", { res, type: payload.type })
        }).catch(err => {
            console.log(err)
        })
    },

    getFindVenueInfo({ commit }, payload) {
        commit("getFindVenueInfo", payload)
    },
    getFindCityInfo({ commit }, payload) {
        commit("getFindCityInfo", payload)
    },

    saveClickTaskRow({ commit }, payload) {
        commit("saveClickTaskRow", payload)
    },

    // 非保障车辆数据
    saveNotGuaranteeAmbAct({ commit }, payload) {
        commit("saveNotGuaranteeAmbMut", payload)
    }
}
const mutations = {
    setToken(state, payload) {
        state.token = payload;
        localStorage.setItem("token", payload);
    },
    saveHospitalAct: (state, payload) => {
        // console.log(payload) 
        let hospitalArr = hospitalList(payload);
        state.hospitalTreeRemoteVx = hospitalArr
        state.hospitalTreeRemoteVxLength = hospitalArr.length
    },
    saveVenueAct: (state, payload) => {
        // console.log(payload) 
        let venueArr = venueList(payload);
        // console.log(venueArr)
        state.venueTreeRemoteVx = venueArr
        // 获取所有医疗点
        state.venueMedicalPointArrVx = []
        venueArr.forEach(item => {
            if (item.children.length > 0) {
                item.children.forEach(itemchild => {
                    state.venueMedicalPointArrVx.push(itemchild)
                })
            }
        })
        // console.log(state.venueMedicalPointArrVx)
        // console.log(state.venueTreeRemoteVx)
    },
    saveAmbulanceAct: (state, payload) => {
        // console.log(payload) 
        // console.log(ambulanceArr)
        // state.ambulanceTreeRemoteVx = ambulanceList(payload)
        state.ambulanceTreeRemoteVx = payload
        // console.log(state.venueTreeRemoteVx)
    },
    saveVenueDetailAct: (state, payload) => {
        let venueDetail = venueDetailMsg(payload)
        state.venueDetailVx = venueDetail
        // console.log(state.venueDetailVx)
    },
    getCityOptionsAct: (state, payload) => {
        state.cityOptionsVx = payload.cityOptions
    },
    getMedicalCount(state, payload) {
        // console.log(payload)
        state.medicalRoomNumVx.total = payload[0].total
        state.medicalRoomNumVx.onLine = payload[0].onLine
        state.FOPMedicalNumVx.total = payload[1].total
        state.FOPMedicalNumVx.onLine = payload[1].onLine
        state.audienceMedicalNumVx.total = payload[2].total
        state.audienceMedicalNumVx.onLine = payload[2].onLine
        state.athletesVillageClinicNumVx.total = payload[3].total
        state.athletesVillageClinicNumVx.onLine = payload[3].onLine
    },
    getAmbulanceCount(state, payload) {
        state.ambulanceNumVx.total = payload[0].total
        state.ambulanceNumVx.onLine = payload[0].onLine
    },
    getUserCount(state, payload) {
        state.doctorNumVx.total = payload[0].total
        state.doctorNumVx.onLine = payload[0].onLine
        state.nurseNumVx.total = payload[1].total
        state.nurseNumVx.onLine = payload[1].onLine
    },
    getHospitalCount(state, payload) {
        state.hospitalNumVx = payload
    },
    getMedicalRoom(state, payload) {
        // console.log(payload)
        let { res, type } = payload
        if (type == 'MedicalPointType-1') {
            if (res?.Children)
                state.medicalRooms = res?.Children
        } else if (type == 'MedicalPointType-2') {
            if (res?.Children)
                state.FOPMedicalPoints = res?.Children
        } else {
            if (res?.Children)
                state.audienceMedicalPoints = res?.Children
        }
    },
    getUserInfo(state, payload) {
        // console.log(payload)
        let { res, type } = payload
        if (type == 5) {
            if (res?.Children)
                state.doctorsVx = res?.Children
        } else {
            if (res?.Children)
                state.nursesVx = res?.Children
        }
    },
    getAmbulanceInfo(state, payload) {
        let { res } = payload
        // console.log(res)
        // state.ambulancesVx = res?.Children
    },
    getAcceptPatient(state, payload) {
        let { res, type } = payload
        // console.log(res)
        if (type == 1)
            state.acceptPatientVx = res
        else
            state.sendHospitalPatientVx = res
    },

    getFindVenueInfo(state, payload) {
        let { findVenueInfo } = payload
        state.venueInfoVx = findVenueInfo
    },
    getFindCityInfo(state, payload) {
        let { findCityInfo } = payload
        state.cityInfoVx = findCityInfo
    },
    saveClickTaskRow(state, payload) {
        state.curTaskRowVx = payload.row
    },
    saveNotGuaranteeAmbMut(state, payload) {
        // console.log('state,payload', state, payload)
        state.notGuaranteeAmbVx = payload
    }
}
/**
 * 类似于计算属性
 */
const getters = {

}
const HospVenuAmbuModule = {
    namespaced: true,
    state,
    mutations,
    actions,
    getters,
}

export default HospVenuAmbuModule